#include <iostream>
#include <vector>
#include <string>
using namespace std;

// 路由表项结构体
struct RouteEntry 
{
    string network;
    int hop_count;
    string next_hop;

    RouteEntry(const string& net, int hop, const string& next)
        : network(net), hop_count(hop), next_hop(next) {}
};

// 函数用于更新路由表
vector<RouteEntry> update_route_table(const vector<RouteEntry>& r1_table, const vector<RouteEntry>& r2_table) 
{
    vector<RouteEntry> updated_r1_table = r1_table;
    for (const auto& entry_r2 : r2_table) 
    {
        bool found = false;
        for (auto& entry_r1 : updated_r1_table) 
        {
            if (entry_r2.network == entry_r1.network) 
            {
                found = true;
                int new_hop_count = entry_r2.hop_count + 1;
                if (new_hop_count < entry_r1.hop_count) 
                {
                    entry_r1.hop_count = new_hop_count;
                    entry_r1.next_hop = entry_r2.next_hop;
                }
                break;
            }
        }
        if (!found) 
        {
            updated_r1_table.emplace_back(entry_r2.network, entry_r2.hop_count + 1, entry_r2.next_hop);
        }
    }
    return updated_r1_table;
}

int main() 
{
    // 路由器 R1 原路由表
    vector<RouteEntry> r1_table = 
    {
        RouteEntry("N1", 1, "R1"),
        RouteEntry("N2", 0, "直接发送"),
        RouteEntry("N3", 6, "R4"),
        RouteEntry("N4", 8, "R5")
    };

    // 路由器 R2 发来的路由表
    vector<RouteEntry> r2_table = 
    {
        RouteEntry("N2", 2, "R5"),
        RouteEntry("N3", 3, "R6"),
        RouteEntry("N4", 7, "R7"),
        RouteEntry("N5", 3, "R8")
    };

    vector<RouteEntry> updated_r1_table = update_route_table(r1_table, r2_table);

    for (const auto& entry : updated_r1_table) 
    {
        cout << "网络: " << entry.network << ", 跳数: " << entry.hop_count << ", 下一跳: " << entry.next_hop << endl;
    }

    return 0;
}